<template>
  <view class="uni-app">
    <view class="status-bar" />
    <view class="main-container">
      <wk-nav-bar :title="title">
        <!-- #ifndef MP-WEIXIN -->
        <button class="button white-btn" @click="handleSave">
          保存
        </button>
        <!-- #endif -->
      </wk-nav-bar>
      <view class="container">
        <wk-form ref="form" :fields="fieldArr" />
      </view>
			
      <!-- #ifdef MP-WEIXIN -->
      <view class="footer-btn-group">
        <button class="button" @click="handleSave">
          保存
        </button>
      </view>
      <!-- #endif -->
    </view>
  </view>
</template>

<script>
  import {
    AddAnnouncement,
    UpdateAnnouncement,
    QueryById
  } from 'API/oa/notice'
  import Fields from '@/utils/fields.js'

  export default {
    name: 'NoticeAdd',
    data() {
      return {
        id: null,
        title: '添加公告',
        detailData: null,

        fieldArr: [
          new Fields({name: '公告标题', formType: 'text', fieldName: 'title', isNull: 1}),
          new Fields({name: '通知人', formType: 'user', fieldName: 'ownerUserIds'}),
          new Fields({name: '通知部门', formType: 'structure', fieldName: 'deptIds'}),
          // new Fields({name: '开始时间', formType: 'date', fieldName: 'startTime', isNull: 1}),
          // new Fields({name: '结束时间', formType: 'date', fieldName: 'endTime', is_Nll: 1}),
          new Fields({name: '公告正文', formType: 'textarea', fieldName: 'content', isNull: 1, maxlength: 2000}),
        ],
        formData: {}
      }
    },
    onLoad(options) {
      this.id = options.id || null
      if (this.id) {
        // this.fieldArr.splice(1, 2)
        this.formData.announcement_id = this.id
        this.title = '编辑公告';
        this.getDetail()
      }
    },
    methods: {
      getDetail() {
        QueryById({
          id: this.id
        }).then(res => {
          let arr = ['title', 'content']
          this.$nextTick(() => {
            arr.forEach(key => {
              this.$refs.form.setFormVal(key, res[key])
            })
            this.$refs.form.setFormVal('ownerUserIds', res.ownerUserList || [])
            this.$refs.form.setFormVal('deptIds', res.deptList)
          })
        }).catch()
      },
      /**
       * 添加或修改公告
       */
      handleSave() {
        this.$refs.form.getForm().then(res => {
          let form = res.entity
          if (this.id) {
            form.announcementId = this.id
          }
          const request = this.id ? UpdateAnnouncement : AddAnnouncement
          // console.log('save: ', form)
          request(form).then(() => {
            this.$toast(this.id ? '修改成功' : '添加成功')
            this.$refreshAndToPrev(this)
          }).catch()
        }).catch(() => {})
      }
    }
  }
</script>

<style scoped lang="scss">
	.main-container {
		display: flex;
		flex-direction: column;
		overflow: hidden;
		
		.container {
			flex: 1;
			padding: 20rpx 0;
		}
	}
	// .app-container {
	// 	/deep/ .textarea-wrapper {
	// 		textarea {
	// 			height: 260px;
	// 		}
	// 	}
	// }
</style>
